﻿Public Class M_UserDao
    Inherits OraDao
    'ユーザテーブルのデータ取得
    Public Function getM_User() As DataTable
        Dim data = New DataTable
        Try
            Dim dao = New OraDao
            Dim sqlBuff = New System.Text.StringBuilder
            sqlBuff.AppendLine("SELECT USERID, PASSWORD, EMPLOYEENAME, DECODE(USERTYPE,'A','管理','C','開発','B','販員') AS USERTYPE, CREATEUSERID, CREATEDAY, UPDATEUSERID, UPDATEDAY")
            sqlBuff.AppendLine("FROM M_USER ")
            sqlBuff.AppendLine("WHERE DELFLG = '0'")

            data = dao.execQuery(sqlBuff.ToString)
        Catch ex As Exception

        End Try
        Return data
    End Function
    'ユーザ情報取得
    Public Function getUserInfo(ByVal userID As String) As DataTable
        Dim data = New DataTable
        Try
            Dim sqlBuff = New System.Text.StringBuilder
            sqlBuff.AppendLine("SELECT USERID,PASSWORD,EMPLOYEENAME,USERTYPE  ")
            sqlBuff.AppendLine("FROM M_USER ")
            sqlBuff.AppendLine("WHERE DELFLG = '0' AND USERID = " & "'" & userID & "'")

            data = Me.execQuery(sqlBuff.ToString)
        Catch ex As Exception

        End Try
        Return data
    End Function
    'ユーザ存在チェック
    Public Function checkExistedUser(ByVal userID As String) As String
        Dim ret As String = ""
        Try
            Dim sqlBuff = New System.Text.StringBuilder
            Dim data = New DataTable
            sqlBuff.AppendLine("SELECT DELFLG As CNT")
            sqlBuff.AppendLine("FROM M_USER ")
            sqlBuff.AppendLine("WHERE USERID = " & "'" & userID & "'")
            sqlBuff.AppendLine("GROUP BY DELFLG ")

            data = Me.execQuery(sqlBuff.ToString)
            If data.Rows.Count <> 0 Then
                If "0".Equals(data.Rows(0)(0)) Then
                    ret = MessageCommon.getMessage("ユーザID", "未削除", My.Resources.E_INPUT_EXISTED)
                Else
                    ret = MessageCommon.getMessage("ユーザID", "削除済", My.Resources.E_INPUT_EXISTED)
                End If
            Else
                ret = ""
            End If

        Catch ex As Exception

        End Try
        Return ret
    End Function
    'ユーザ追加
    Public Function insertUser(ByVal userInfo As User, ByVal operatorID As String) As String
        Dim ret As Integer
        Try
            Dim retMsg = checkExistedUser(userInfo.userID)
            If Not retMsg.Equals("") Then
                Return retMsg
            End If

            Dim sqlBuff = New System.Text.StringBuilder
            sqlBuff.AppendLine("INSERT INTO M_USER (")
            sqlBuff.AppendLine("USERID ")
            sqlBuff.AppendLine(",PASSWORD ")
            sqlBuff.AppendLine(",EMPLOYEENAME ")
            sqlBuff.AppendLine(",USERTYPE ")
            sqlBuff.AppendLine(",CREATEUSERID ")
            sqlBuff.AppendLine(",CREATEDAY ")
            sqlBuff.AppendLine(",UPDATEUSERID ")
            sqlBuff.AppendLine(",UPDATEDAY ")
            sqlBuff.AppendLine(",DELFLG )")
            sqlBuff.AppendLine(" VALUES ( ")
            sqlBuff.AppendLine("'" & userInfo.userID & "'")
            sqlBuff.AppendLine(",'" & userInfo.password & "'")
            sqlBuff.AppendLine(",'" & userInfo.userName & "'")
            sqlBuff.AppendLine(",'" & userInfo.userType & "'")
            sqlBuff.AppendLine(",'" & operatorID & "'")
            sqlBuff.AppendLine(",'" & DateTime.Today & "'")
            sqlBuff.AppendLine(",'" & operatorID & "'")
            sqlBuff.AppendLine(",'" & DateTime.Today & "'")
            sqlBuff.AppendLine(",'0' )")
            ret = Me.execNoneQuery(sqlBuff.ToString)
        Catch ex As Exception
            Return My.Resources.E_SYSTEM_FAIL
        End Try
        If ret = 0 Then
            Return MessageCommon.getMessage("追加", My.Resources.E_ACTION_FAILED)
        Else
            Return ""
        End If
    End Function
    'ユーザ更新
    Public Function updateUser(ByVal userInfo As User, ByVal operatorID As String) As String
        Dim ret As Integer
        Try
            Dim sqlBuff = New System.Text.StringBuilder
            sqlBuff.AppendLine("UPDATE M_USER ")
            sqlBuff.AppendLine("SET EMPLOYEENAME = " & "'" & userInfo.userName & "'")
            sqlBuff.AppendLine(",PASSWORD = " & "'" & userInfo.password & "'")
            sqlBuff.AppendLine(",USERTYPE = " & "'" & userInfo.userType & "'")
            sqlBuff.AppendLine(",UPDATEUSERID = " & "'" & operatorID & "'")
            sqlBuff.AppendLine(",UPDATEDAY = " & "'" & DateTime.Today & "'")
            sqlBuff.AppendLine(" WHERE USERID = " & "'" & userInfo.userID & "'")
            Debug.Write(sqlBuff.ToString)
            ret = Me.execNoneQuery(sqlBuff.ToString)
        Catch ex As Exception
            Return My.Resources.E_SYSTEM_FAIL
        End Try
        If ret = 0 Then
            Return MessageCommon.getMessage("編集", My.Resources.E_ACTION_FAILED)
        Else
            Return ""
        End If
    End Function
    'ユーザ削除
    Public Function deleteUser(ByVal userID As String, ByVal operatorID As String) As String
        Dim ret As Integer
        Try
            Dim sqlBuff = New System.Text.StringBuilder
            sqlBuff.AppendLine("UPDATE M_USER ")
            sqlBuff.AppendLine("SET DELFLG = '1' ")
            sqlBuff.AppendLine(",UPDATEUSERID = " & "'" & operatorID & "'")
            sqlBuff.AppendLine(",UPDATEDAY = " & "'" & DateTime.Today & "'")
            sqlBuff.AppendLine(" WHERE USERID = " & "'" & userID & "'")
            ret = Me.execNoneQuery(sqlBuff.ToString)
        Catch ex As Exception
            Return My.Resources.E_SYSTEM_FAIL
        Finally
        End Try
        If ret = 0 Then
            Return MessageCommon.getMessage("削除", My.Resources.E_ACTION_FAILED)
        Else
            Return ""
        End If
    End Function
End Class